package board

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"notabug.org/ropemann/imabon/src/database"
)

type Board struct {
	ID        *primitive.ObjectID `json:"ID" bson:"_id,omitempty"`
	Name      string              `json:"Name" bson:"name,omitempty"`
	Category  string              `json:"Category" bson:"category,omitempty"`
	Path      string              `json:"Path" bson:"path,omitempty"`
	CreatedAt *primitive.DateTime `json:"CreatedAt" bson:"createdAt,omitempty"`
	UpdatedAt *primitive.DateTime `json:"UpdatedAt" bson:"updatedAt,omitempty"`
}

func GetAllBoards() ([]Board, error) {
	db := database.NewMongoDBConn()
	db.Connect()

	collection := db.Client.Database("site").Collection("boards")
	cur, currErr := collection.Find(db.Ctx, bson.D{})
	boards := []Board{}

	if currErr != nil {
		return boards, currErr
	}
	defer cur.Close(db.Ctx)
	defer db.Close()

	if err := cur.All(db.Ctx, &boards); err != nil {
		return boards, err
	}

	return boards, nil
}
